/**
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via
 * e-mail: ralf.vandenhouten@th-wildau.de.
 */
package de.thwildau.tm.moses.abnf.generator;

import com.google.common.base.Objects;
import de.thwildau.tm.moses.abnf.abnf.Rule;
import de.thwildau.tm.moses.abnf.generator.model.AbnfAlternation;
import de.thwildau.tm.moses.abnf.generator.model.AbnfCharValue;
import de.thwildau.tm.moses.abnf.generator.model.AbnfRuleClass;
import de.thwildau.tm.moses.abnf.generator.model.impl.AbnfRuleClassImpl;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.xbase.lib.Functions.Function0;

/**
 * This Xtend class provides standard templates for Java class files.
 */
@SuppressWarnings("all")
public class JavaTemplates {
  private static Pattern FIELD_NAME_REPLACEMENTS = new Function0<Pattern>() {
    public Pattern apply() {
      Pattern _compile = Pattern.compile("\\p{Punct}");
      return _compile;
    }
  }.apply();
  
  private static Pattern STARTS_WITH_DIGIT = new Function0<Pattern>() {
    public Pattern apply() {
      Pattern _compile = Pattern.compile("\\d.");
      return _compile;
    }
  }.apply();
  
  public static CharSequence generatePackage(final QualifiedName fqn) {
    StringConcatenation _builder = new StringConcatenation();
    String packageName = JavaTemplates.getPackageName(fqn);
    _builder.newLineIfNotEmpty();
    {
      boolean _isEmpty = packageName.isEmpty();
      boolean _not = (!_isEmpty);
      if (_not) {
        _builder.append("package ");
        _builder.append(packageName, "");
        _builder.append(";");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.newLine();
    return _builder;
  }
  
  public static CharSequence enumFieldDelimiter(final boolean isLastField) {
    StringConcatenation _builder = new StringConcatenation();
    {
      if (isLastField) {
        _builder.append(";");
      } else {
        _builder.append(",");
      }
    }
    return _builder;
  }
  
  /**
   * Returns the package name of the FQN. Returns an empty string if the FQN does
   * not have a package part. Never returns null.
   */
  public static String getPackageName(final QualifiedName fqn) {
    boolean _and = false;
    boolean _notEquals = (!Objects.equal(fqn, null));
    if (!_notEquals) {
      _and = false;
    } else {
      int _segmentCount = fqn.getSegmentCount();
      boolean _greaterThan = (_segmentCount > 1);
      _and = (_notEquals && _greaterThan);
    }
    if (_and) {
      QualifiedName _skipLast = fqn.skipLast(1);
      return _skipLast.toString();
    } else {
      return "";
    }
  }
  
  public static String toFieldName(final AbnfCharValue charValue) {
    boolean _equals = Objects.equal(charValue, null);
    if (_equals) {
      return null;
    }
    String value = charValue.getValue();
    boolean _or = false;
    boolean _equals_1 = Objects.equal(value, null);
    if (_equals_1) {
      _or = true;
    } else {
      boolean _isEmpty = value.isEmpty();
      _or = (_equals_1 || _isEmpty);
    }
    if (_or) {
      return null;
    }
    Matcher _matcher = JavaTemplates.FIELD_NAME_REPLACEMENTS.matcher(value);
    String _replaceAll = _matcher.replaceAll("");
    String _trim = _replaceAll.trim();
    return _trim.toLowerCase();
  }
  
  public static String toEnumName(final String value) {
    Matcher _matcher = JavaTemplates.FIELD_NAME_REPLACEMENTS.matcher(value);
    String _replaceAll = _matcher.replaceAll("_");
    String enumName = _replaceAll.toUpperCase();
    Matcher _matcher_1 = JavaTemplates.STARTS_WITH_DIGIT.matcher(enumName);
    boolean _find = _matcher_1.find();
    if (_find) {
      String _plus = ("V" + enumName);
      enumName = _plus;
    }
    return enumName;
  }
  
  public static CharSequence generateFileComment() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("/***********************************************************");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("* This class was generated by the MOSES ABNF Compiler.");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("* Created: ");
    DateFormat _instance = DateFormat.getInstance();
    Date _date = new Date();
    String _format = _instance.format(_date);
    _builder.append(_format, " ");
    _builder.newLineIfNotEmpty();
    _builder.append(" ");
    _builder.append("*");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("* (c) ");
    Calendar _instance_1 = Calendar.getInstance();
    int _get = _instance_1.get(Calendar.YEAR);
    _builder.append(_get, " ");
    _builder.append(" Technische Hochschule Wildau");
    _builder.newLineIfNotEmpty();
    _builder.append(" ");
    _builder.append("* (University of Applied Sciences Wildau)");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("* All rights reserved");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("***********************************************************/");
    _builder.newLine();
    return _builder;
  }
  
  public static CharSequence generateClassComment(final AbnfRuleClass rc) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("/**");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("* This is a generated data type class for the ABNF rule \'");
    Rule _rule = ((AbnfRuleClassImpl) rc).getRule();
    String _name = _rule.getName();
    _builder.append(_name, " ");
    _builder.append("\'.");
    _builder.newLineIfNotEmpty();
    _builder.append(" ");
    _builder.append("* ");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("* @generated");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("*/");
    _builder.newLine();
    return _builder;
  }
  
  public static CharSequence generateClassComment(final AbnfAlternation<? extends Object> alternation) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("/**");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("* This is a generated enum data type class for the ABNF alternation \'");
    String _name = alternation.getName();
    _builder.append(_name, " ");
    _builder.append("\'.");
    _builder.newLineIfNotEmpty();
    _builder.append(" ");
    _builder.append("* ");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("* @generated");
    _builder.newLine();
    _builder.append(" ");
    _builder.append("*/");
    _builder.newLine();
    return _builder;
  }
}
